/** header **/

#include "my_thread_list.h"

/** thread_t list data structure **/
struct __thread_t_list {
  struct __thread_t_list * next;
  thread_t content;
};

/**********************************************************************************************
 * __thread_t_list_empty
 *********************************************************************************************/
thread_t_list __thread_t_list_empty () {
  return NULL;
}

/**********************************************************************************************
 * __thread_t_list_new
 *********************************************************************************************/
thread_t_list __thread_t_list_new (thread_t _thread_t) {
  thread_t_list list = (thread_t_list) malloc (sizeof (struct __thread_t_list));
  
  if (list == NULL)
    __internal_error (ERROR_MEMORY_ALLOCATION, __FILE__, __func__,__LINE__);
  
  list->next = NULL;
  list->content = _thread_t;
  return list;
}

/**********************************************************************************************
 * __thread_t_list_delete
 *********************************************************************************************/
void __thread_t_list_delete (thread_t_list list) {
  if (list != NULL) {
    __thread_t_list_delete (list->next);
    list->next = NULL;
    free (list);
  }
  return ;
}

/**********************************************************************************************
 * __thread_t_list_append
 *********************************************************************************************/
thread_t_list __thread_t_list_append (thread_t first, thread_t second) {
  thread_t_list first_list = __thread_t_list_new (first);
  thread_t_list second_list = __thread_t_list_new (second);  
  
  if (first_list == NULL || second_list == NULL)
    __internal_error (ERROR_NULL_POINTER, __FILE__, __func__,__LINE__);
  
  first_list->next = second_list;
  
  return first_list;
}

/**********************************************************************************************
 * __thread_t_list_insert
 *********************************************************************************************/
thread_t_list __thread_t_list_insert (thread_t_list first, thread_t _thread_t) {
  thread_t_list second = __thread_t_list_new (_thread_t);

  if (second == NULL)
    __internal_error (ERROR_NULL_POINTER, __FILE__, __func__,__LINE__);
  
  if (first == NULL) {
    second->content = _thread_t;
    return second;

  } else if (first->next != NULL) {
    __thread_t_list_insert (first->next, _thread_t);

  } else {
    first->next = second;
  }
  return first;
}
/**********************************************************************************************
 * __thread_t_list_remove
 *********************************************************************************************/
thread_t_list __thread_t_list_remove (thread_t_list first, thread_t _thread_t){
	thread_t_list_iterator prev=__thread_t_list_iterator(first);
	thread_t_list_iterator curr=__thread_t_list_iterator(first);
	while(!__thread_t_list_iterator_end(curr)){
		if(_thread_t_list_iterator_current(curr)==_thread_t){
			prev->next=__thread_t_list_iterator_next(curr);
			return first;
		}
		prev=curr;
		curr=__thread_t_list_iterator_next(curr);

	}
	return first;
}
/**********************************************************************************************
 * __thread_t_list_concatenate
 *********************************************************************************************/
thread_t_list __thread_t_list_concatenate (thread_t_list first, thread_t_list second) {
  if (first == NULL) return second;

  thread_t_list current = first;
  while (current->next != NULL) current = current->next;
  
  current->next = second;
  return first;
}

/**********************************************************************************************
 * __thread_t_list_iterator_new
 *********************************************************************************************/
thread_t_list_iterator __thread_t_list_iterator (thread_t_list list) {
  return (thread_t_list_iterator) list;
}

/**********************************************************************************************
 * __thread_t_list_iterator_current
 *********************************************************************************************/
thread_t __thread_t_list_iterator_current (thread_t_list_iterator iterator) {
  if (iterator == NULL)
    __internal_error (ERROR_NULL_POINTER, __FILE__, __func__,__LINE__);
  return iterator->content;
}

/**********************************************************************************************
 * __thread_t_list_iterator_next
 *********************************************************************************************/
thread_t_list_iterator __thread_t_list_iterator_next (thread_t_list_iterator iterator) {
  if (iterator == NULL)
    __internal_error (ERROR_NULL_POINTER, __FILE__, __func__,__LINE__);

  return (thread_t_list_iterator) iterator->next;
}

/**********************************************************************************************
 * __thread_t_list_iterator_end
 *********************************************************************************************/
__boolean __thread_t_list_iterator_end (thread_t_list_iterator iterator) {
  return (iterator == NULL);
}
